package com.woniuxy.dudugua.controller;

import com.woniuxy.dudugua.entity.*;
import com.woniuxy.dudugua.service.ProductOperationService;
import com.woniuxy.dudugua.util.Result;
import com.woniuxy.dudugua.util.qnyutil.QiniuUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("product")
public class ProductOperationController {
    @Resource
    private ProductOperationService productOperationService;
    @Resource
    private QiniuUtil qiniuUtil;
    @RequestMapping("addProduct")
    public Result addProduct(Product product, HttpSession session)throws Exception{
        Person person = (Person) session.getAttribute("person");
        product.setStoreId(person.getStoreId());
        Product product1 = productOperationService.addProduct(product);
        session.setAttribute("productId",product1.getId());
        return new Result("200","success",product1,null);
    }
    @RequestMapping("addIntroduce")
    public Result addIntroduce(String introduce, HttpSession session)throws Exception{
        Integer productId = (Integer) session.getAttribute("productId");
        ProductInfo productInfo = new ProductInfo();
        productInfo.setIntroduce(introduce);
        productInfo.setProductId(productId);
        //调用存入方法
        productOperationService.addIntroduce(productInfo);
        return new Result("200","success",null,null);
    }
    @RequestMapping("addSKU")
    public Result addSKU(@RequestBody List<ProductSku> productSkuList,HttpSession session)throws Exception{
        Integer productId = (Integer) session.getAttribute("productId");
        int size = productSkuList.size();
        for(ProductSku productSku:productSkuList){
            //如果某个数据提交过来是空，则不提交
            if(productSku.getProperty()==""||productSku.getNumber()==0||productSku.getPrice()==0){
                continue;
            }
            productSku.setProductId(productId);
            productOperationService.addSku(productSku);
        }
        return new Result("200","success",null,null);
    }
    /**
     * 上传单张图片
     */
    @RequestMapping("addProductImage")
    public Result addProductImages(@RequestParam("file") MultipartFile file, @RequestParam("status") Integer status, HttpSession session) throws Exception {
        Integer productId = (Integer) session.getAttribute("productId");
        String filename="product/"+ UUID.randomUUID().toString();
        qiniuUtil.upload(filename,file.getInputStream());
        String path = qiniuUtil.fullPath(filename);
        ProductImages productImages = new ProductImages();
        productImages.setProductId(productId);
        productImages.setUrl(path);
        productImages.setStatus(1);
        productOperationService.addProductImage(productImages);
        return new Result("200","success",null,null);
    }
    /**
     * 上传多张商品图片
     */
    @RequestMapping("addProductImages")
    public Result addProductImages(@RequestParam("file") MultipartFile[] files, @RequestParam("status") Integer status, HttpSession session) throws Exception {
        Integer productId = (Integer) session.getAttribute("productId");
        for(MultipartFile file:files ){
            String filename="product/"+ UUID.randomUUID().toString();
            qiniuUtil.upload(filename,file.getInputStream());
            String path = qiniuUtil.fullPath(filename);
            ProductImages productImages = new ProductImages();
            productImages.setProductId(productId);
            productImages.setUrl(path);
            productImages.setStatus(2);
            productOperationService.addProductImage(productImages);
        }
        return new Result("200","success",null,null);
    }

    /**
     * 上传多张商品详情图片
     */
    @RequestMapping("addProductInfoImages")
    public Result addProductInfoImages(@RequestParam("file") MultipartFile[] files, HttpSession session) throws Exception {
        Integer productId = (Integer) session.getAttribute("productId");
        int index=1;
        for(MultipartFile file:files ){
            String filename="product/"+ UUID.randomUUID().toString();
            qiniuUtil.upload(filename,file.getInputStream());
            String path = qiniuUtil.fullPath(filename);
            ProductInfoImages productInfoImages = new ProductInfoImages();
            productInfoImages.setProductId(productId);
            productInfoImages.setUrl(path);
            productInfoImages.setIndex(index++);
            productOperationService.addProductInfoImage(productInfoImages);
        }
        return new Result("200","success",null,null);
    }
}
